import javax.swing.tree.TreeNode;
import java.util.Stack;

public class TestDemo14 {

    public static void main(String[] args) {
        String s="bxj##tw";
        String t="bxj###tw";
        System.out.println(backspaceCompare(s,t));
    }
    public static boolean backspaceCompare(String s, String t){
        Stack<Character>stack1=new Stack<>();
        Stack<Character>stack2=new Stack<>();
        for(int i=0;i<s.length();i++) {
            char ch = s.charAt(i);
            if (ch != '#') {
                stack1.push(ch);
            } else {
                if (stack1.empty()) {

                } else {
                    stack1.pop();
                }
            }
        }
        for(int i=0;i<t.length();i++){
            char ch=t.charAt(i);
            if(ch!='#'){
                stack2.push(ch);
            }
            else{
                if (stack2.empty()) {

                } else {
                    stack2.pop();
                }
            }
        }while(stack1.empty()==false&&stack2.empty()==false){
            if(stack1.pop()!=stack2.pop()){
                return false;
            }
        }if(stack1.empty()==false||stack2.empty()==false){
            return false;
        }else{
        return true;
    }
    }
    public static void main1(String[] args) {
        String []ops={"5","2","C","D","+"};
        System.out.println(calPoints(ops));
    }
    public static int calPoints(String[] ops){
        Stack<Integer>stack=new Stack<>();
        int sum=0;
        for(int i=0;i<ops.length;i++){
            String val=ops[i];
            if(!(val.equals("+")||val.equals("C")||val.equals("D"))){
                stack.push(Integer.parseInt(val));
            }else{
                switch (val){
                    case"C":
                        stack.pop();
                        break;
                    case "D":
                        stack.push(stack.peek()*2);
                        break;
                    case "+":
                        int top=stack.pop();
                        int newHead=top+stack.peek();
                        stack.push(top);
                        stack.push(newHead);
                        break;
                }
            }
        }while (!stack.empty()){
            sum=sum+stack.peek();
            stack.pop();
        }return sum;
    }
    public boolean IsPopOrder(int [] pushA,int [] popA){
        Stack<Integer>stack=new Stack<>();
        int j=0;
        for(int i=0;i<pushA.length;i++){
            stack.push(pushA[i]);
            while(j<popA.length&&!stack.empty()&&stack.peek()==popA[j]){
                stack.pop();
                j++;
            }
        }return stack.empty();
    }
    public int evalRPN(String[] tokens){
        Stack<Integer>stack=new Stack<>();
        for(int i=0;i<tokens.length;i++){
           String val=tokens[i];
           if(!isOperation(val)){
              stack.push(Integer.parseInt(val));//把String转换成Integer
           }else{
               int num2=stack.pop();
               int num1=stack.pop();
               switch (val){
                   case"+":
                       stack.push(num1+num2);
                       break;
                   case"-":
                       stack.push(num1-num2);
                       break;
                   case"*":
                       stack.push(num1*num2);
                       break;
                   case"/":
                       stack.push(num1/num2);
                       break;

               }
           }
        }return stack.pop();
    }
    private boolean isOperation(String x) {
        if(x.equals('+')||x.equals('-')||x.equals('*')||x.equals('/')){
            return true;
        }return false;
    }

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            } else {
                if (stack.empty()) {
                    System.out.println("右边括号多");
                    return false;
                }
                char top = stack.peek();
                if (top =='{'&&ch=='}'||top=='('&&ch==')'||top=='['&&ch==']'){
                    stack.pop();
                }
                else{
                    return false;
                }
            }
        }if(!stack.empty()){
            System.out.println("左边括号多");
            return false;
        }return true;
    }

}
